﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AutoTLBB.Utilites;

namespace AutoTLBB
{
	/// <summary>
	/// Chart User Info Control
	/// </summary>
	public partial class CharacterInfoControl : UserControl
	{
		#region Fields
		private int m_maxMana = 0;
		private int m_currentMana = 0;
		private int m_maxBlood;
		private int m_currentBlood;
		private int m_maxPetBlood;
		private int m_currentPetBlood;
		public event EventHandler RequestCloseGame;
		private CharacterModel m_dataContext;
		private BackgroundWorker m_bgwBlood;
		private BackgroundWorker m_statusBackgroundWorker;
		private int m_yLocation = 24;
		private string m_statusMessage = "";
		private int m_statusCount = 0;
		private object m_locationLocker = new object();
		#endregion Fields
		#region Constructor
		public CharacterInfoControl()
		{
			InitializeComponent();
			m_cbxChonMucTieu.DataSource = Enum.GetValues(typeof(UserKey));
			m_bgwBlood = new BackgroundWorker();
			m_statusBackgroundWorker = new BackgroundWorker();
			m_bgwBlood.DoWork += new DoWorkEventHandler(CalculateBloodAndMana);
			m_bgwBlood.WorkerReportsProgress = true;
			m_bgwBlood.ProgressChanged += new ProgressChangedEventHandler(ReportBlood);
			m_statusBackgroundWorker.DoWork += new DoWorkEventHandler(OnStatusUpdate);
			m_statusBackgroundWorker.WorkerReportsProgress = true;
			m_statusBackgroundWorker.ProgressChanged += new ProgressChangedEventHandler(OnUpdateStatus);
		}
		#endregion Constructor
		#region Methods
		/// <summary>
		/// Called when [update status].
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.ComponentModel.ProgressChangedEventArgs"/> instance containing the event data.</param>
		void OnUpdateStatus(object sender, ProgressChangedEventArgs e)
		{
			lock (m_locationLocker)
			{
				m_lblStatus.Location = new Point(m_lblStatus.Location.X, m_yLocation);
				m_lblStatus.Text = m_statusMessage;
			}
		}
		/// <summary>
		/// Called when [status update].
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param>
		void OnStatusUpdate(object sender, DoWorkEventArgs e)
		{
			BackgroundWorker worker = sender as BackgroundWorker;
			while (true)
			{
				if ((worker.CancellationPending == true))
				{
					e.Cancel = true;
					break;
				}
				else
				{
					lock (m_locationLocker)
					{
						m_yLocation -= 1;
						if (m_yLocation == -20)
						{
							m_yLocation = 24;
							if (m_statusCount == 0)
							{
								m_statusMessage = "Câu hỏi auto đã xuất hiện : " +
								Convert.ToInt32(DateTime.Now.Subtract(m_dataContext.TimeAutoHandle).TotalMinutes) + " phút";
								m_statusCount += 1;
							}
							else
							{
								m_statusMessage = "Nhận viết phần mềm, website liên hệ: chithanh_12@yahoo.com";
								m_statusCount = 0;
							}
						}
						System.Threading.Thread.Sleep(100);
						worker.ReportProgress((10));
					}
				}
			}
		}
		/// <summary>
		/// Reports the blood.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.ComponentModel.ProgressChangedEventArgs"/> instance containing the event data.</param>
		void ReportBlood(object sender, ProgressChangedEventArgs e)
		{
			m_bloodCharacter.MaxBlood = m_maxBlood;
			m_bloodCharacter.MaxMana = m_maxMana;
			m_bloodCharacter.CurrentBlood = m_currentBlood;
			m_bloodCharacter.CurrentMana = m_currentMana;

			m_bloodPetControl.MaxBlood = m_maxPetBlood;
			m_bloodPetControl.CurrentBlood = m_currentPetBlood;
			m_lblCharacterBlood.Text = m_currentBlood + "/" + m_maxBlood;
			m_lblCharacterMana.Text = m_currentMana + "/" + m_maxMana;
			m_lblPetBlood.Text = m_currentPetBlood + "/" + m_maxPetBlood;
			//m_lblStatus.Text = "Câu hỏi auto đã xuất hiện : " +
			//    Convert.ToInt32(DateTime.Now.Subtract(m_dataContext.TimeAutoHandle).TotalMinutes) + " phút";
		}
		/// <summary>
		/// Calculates the blood and mana.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param>
		void CalculateBloodAndMana(object sender, DoWorkEventArgs e)
		{
			BackgroundWorker worker = sender as BackgroundWorker;
			while (true)
			{
				if (m_dataContext != null)
				{
					m_currentBlood = GameUtilities.GetCharacterBlood(m_dataContext.ProcessId);
					m_currentMana = GameUtilities.GetCharacterMana(m_dataContext.ProcessId);
					m_maxBlood = GameUtilities.GetMaxCharacterBlood(m_dataContext.ProcessId);
					m_maxMana = GameUtilities.GetMaxCharacterMana(m_dataContext.ProcessId);
					m_maxPetBlood = GameUtilities.GetMaxPetBlood(m_dataContext.ProcessId);
					m_currentPetBlood = GameUtilities.GetPetBlood(m_dataContext.ProcessId);
				}

				if ((worker.CancellationPending == true))
				{
					e.Cancel = true;
					break;
				}
				else
				{
					// Perform a time consuming operation and report progress.
					System.Threading.Thread.Sleep(1500);
					worker.ReportProgress((10));
				}
			}

		}
		/// <summary>
		/// Handles the Load event of the CharacterInfoControl control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void CharacterInfoControl_Load(object sender, EventArgs e)
		{

		}
		/// <summary>
		/// Sets the data context.
		/// </summary>
		/// <param name="model">The model.</param>
		public void SetDataContext(CharacterModel model)
		{
			m_dataContext = model;
			if (model == null)
			{
				return;
			}
			m_basicSkill.SetDataContext(model.BasicSkill);
			m_skill1.SetDataContext(model.Skill1);
			m_skill2.SetDataContext(model.Skill2);
			m_skill3.SetDataContext(model.Skill3);
			m_skill4.SetDataContext(model.Skill4);
			m_skill5.SetDataContext(model.Skill5);
			m_skill6.SetDataContext(model.Skill6);
			m_skill7.SetDataContext(model.Skill7);
			m_eatingBlood.SetDataContext(model.EatingBlood);
			m_eatingMana.SetDataContext(model.EatingMana);
			m_petFeed.SetDataContext(model.PetFeed);
			m_eatingWhen.SetDataContext(model.EatingWhenBloodOfPet);

			m_rdbNgoaiStyle.Checked = m_dataContext.HitStyle == HitStyle.Ngoai;
			m_rdbNoiStyle.Checked = m_dataContext.HitStyle == HitStyle.Noi;
			m_rdbEachMonsterStyle.Checked = m_dataContext.HitStyle == HitStyle.ByMonster;
			m_txtByMonsterDelay.Text = m_dataContext.TimeDelayToAttack.ToString();

			m_alertBloodControl.SetDataContext(model.AlertBlood);
			m_alertRadiusControl.SetDataContext(model.Radius);
			m_shutDownAfterQuestion.SetDataContext(model.ShutdownGameAfterQuestion);
			m_shutdownWhenBlood.SetDataContext(model.ShutdownGameWhenBlood);
			m_cbxChonMucTieu.SelectedItem = model.Target;
			m_chkHideGame.Checked = model.IsHideGame;
			if (m_bgwBlood.IsBusy != true)
			{
				m_bgwBlood.RunWorkerAsync();
			}
			if (!m_statusBackgroundWorker.IsBusy)
			{
				m_statusBackgroundWorker.RunWorkerAsync();
			}
			RefreshStartButton();
		}
		/// <summary>
		/// Called when [start stop auto].
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void OnStartStopAuto(object sender, EventArgs e)
		{
			if (m_dataContext == null) { return; }
			m_dataContext.IsRunning = !m_dataContext.IsRunning;
			if (m_dataContext.IsRunning)
			{
				m_dataContext.LocationX = GameUtilities.GetLocationX(m_dataContext.ProcessId);
				m_dataContext.LocationY = GameUtilities.GetLocationY(m_dataContext.ProcessId);
			}
			RefreshStartButton();
		}
		/// <summary>
		/// Targets the key changed.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void TargetKeyChanged(object sender, EventArgs e)
		{
			if (m_dataContext == null) { return; }
			m_dataContext.Target = (UserKey)m_cbxChonMucTieu.SelectedItem;
		}
		/// <summary>
		/// Hits the style changed.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void HitStyleChanged(object sender, EventArgs e)
		{
			if (m_dataContext == null) { return; }
			if (m_rdbNgoaiStyle.Checked)
			{
				m_dataContext.HitStyle = HitStyle.Ngoai;
			}
			else if (m_rdbNoiStyle.Checked)
			{
				m_dataContext.HitStyle = HitStyle.Noi;
			}
			else
			{
				m_dataContext.HitStyle = HitStyle.ByMonster;
				m_dataContext.TimeDelayToAttack = Int32.Parse(m_txtByMonsterDelay.Text);
			}
		}
		/// <summary>
		/// Hiddens the game.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void HiddenGame(object sender, EventArgs e)
		{
			if (m_dataContext == null) { return; }
			m_dataContext.IsHideGame = m_chkHideGame.Checked;
			GameUtilities.ShowGame(m_dataContext.WindowHandle, m_dataContext.IsHideGame);
		}
		/// <summary>
		/// Refreshes the start button.
		/// </summary>
		public void RefreshStartButton()
		{
			if (m_dataContext == null) { return; }
			m_bntStart.Text = m_dataContext.IsRunning ? "Stop" : "Start";
			m_bntStart.BackColor = m_dataContext.IsRunning ? Color.Green : Color.Red;
		}
		/// <summary>
		/// Updates the hide game.
		/// </summary>
		/// <param name="isHide">if set to <c>true</c> [is hide].</param>
		public void UpdateHideGame(bool isHide)
		{
			this.m_chkHideGame.Checked = isHide;
		}
		/// <summary>
		/// Saves the model config.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void SaveModelConfig(object sender, EventArgs e)
		{
			if (m_dataContext != null)
			{
				try
				{
					XmlSerializeHelper.SerializeUserConfig(m_dataContext);
					MessageBox.Show("Lưu cấu hình cho nhân vật thành công!");
				}
				catch (Exception ex)
				{
					MessageBox.Show("Có lỗi xẩy ra trong quá trình lưu! Bạn hãy thử lại lần nữa xem sao!");
				}
			}
		}
		/// <summary>
		/// Turns the off game click.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void TurnOffGameClick(object sender, EventArgs e)
		{
			if (MessageBox.Show("Bạn muốn thoát game?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
			{
				if (RequestCloseGame != null)
				{
					RequestCloseGame(this, EventArgs.Empty);
				}
			}
		}
		#endregion Methods
	}
}
